﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TCOChart
{
    public class LogHelper
    {

        private RichTextBox _richTextBox;

        public LogHelper(RichTextBox richTextBox)
        {
            this._richTextBox = richTextBox;
        }

        private void LogMessage(string message, LogLevel logLevel)
        {
            _richTextBox.SelectionStart = _richTextBox.TextLength;
            _richTextBox.SelectionLength = 0;

            switch (logLevel)
            {
                case LogLevel.Info:
                    _richTextBox.SelectionColor = Color.Black;
                    break;
                case LogLevel.Error:
                    _richTextBox.SelectionColor = Color.Red;
                    break;
            }

            _richTextBox.AppendText(message);
            _richTextBox.SelectionColor = _richTextBox.ForeColor;
            _richTextBox.ScrollToCaret();
        }


        public void Log(string message, LogLevel logLevel = LogLevel.Info)
        {
            string logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {logLevel.ToString().ToUpper()} - {message}{Environment.NewLine}";
            if (_richTextBox.InvokeRequired)
                _richTextBox.Invoke(new MethodInvoker(() => LogMessage(logMessage, logLevel)));
            else
            {
                LogMessage(logMessage, logLevel);
            }
        }
    }
    public enum LogLevel
    {
        Info,
        Error
    }

}
